package com.superzlc.vcontroller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class CtrlAllView extends ViewGroup {

	public CtrlAllView(Context context) {
		super(context);
		tv = new TextView(context);
		tv.setText("hello");


		for (int i = 0; i < 8; i++) {
			SeekBar axis = new SeekBar(this.getContext());
			axis.setMax(32767);
			axis.setId(INDEX_START_ASIX + i);
			axis.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

				@Override
				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser) {
					asixDrag(seekBar.getId(), progress);
				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
					asixDrag(seekBar.getId());
				}

				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
					asixDrag(seekBar.getId());
				}

			});
			asixList.add(axis);
			this.addView(axis);
		}
		for (int i = 0; i < 32; i++) {
			Button btn = new Button(this.getContext());
			btn.setText(String.valueOf(i + 1));
			btn.setId(INDEX_START_BTN + i);
			btn.setOnTouchListener(new OnTouchListener(){
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					switch(event.getAction() & MotionEvent.ACTION_MASK) {
					case MotionEvent.ACTION_DOWN:
						btnPressDown(v.getId());
						break;
					case MotionEvent.ACTION_UP:
						btnPressUp(v.getId());
						break;
					}
					return true;
				}
			});
			btnList.add(btn);
			this.addView(btn);
		}
	}


	private static final int INDEX_START_BTN = 1000;
	private static final int INDEX_START_ASIX = 2000;
	private final List<SeekBar> asixList = new ArrayList<>();
	private final List<Button> btnList = new ArrayList<>();
	private TextView tv;
	private final int asixW = 480, asixH = 100;
	private final int btnW = 140, btnH = 140;
	private final int[][] pp1 = new int[][]{
			{40, 40},
			{40, 140},
			{40, 240},

			{580, 40},
			{580, 140},
			{580, 240},

			{40, 360},
			{40, 460},
	};

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		for (int i = 0; i < asixList.size(); i++) {
			SeekBar axis = asixList.get(i);
			LayoutParams1 lp = (LayoutParams1)axis.getLayoutParams();
			lp.width = asixW;
			lp.height = asixH;
			lp.left = pp1[i][0];
			lp.top = pp1[i][1];
		}
		for (int i = 0; i < btnList.size(); i++) {
			Button btn = btnList.get(i);
			LayoutParams1 lp = (LayoutParams1)btn.getLayoutParams();
			lp.width = btnW;
			lp.height = btnH;
			lp.left = (i % 6 * (btnW + 10));
			lp.top = 600 + (i / 6 * (btnH + 10));
		}
		// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		this.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		for (int i = 0; i < asixList.size(); i++) {
			SeekBar axis = asixList.get(i);
			LayoutParams1 lp = (LayoutParams1)axis.getLayoutParams();
			axis.layout(lp.left, lp.top, lp.left + lp.width, lp.top + lp.height);
		}
		for (int i = 0; i < btnList.size(); i++) {
			Button btn = btnList.get(i);
			LayoutParams1 lp = (LayoutParams1)btn.getLayoutParams();
			btn.layout(lp.left, lp.top, lp.left + lp.width, lp.top + lp.height);
		}
	}


	/////////////////////
	static class LayoutParams1 extends LayoutParams {
		int left;
		int top;
		public LayoutParams1(Context c, AttributeSet attrs) {
			super(c, attrs);
		}

		public LayoutParams1(LayoutParams source) {
			super(source);
		}

		public LayoutParams1(int width, int height) {
			super(width, height);
		}
	}

	@Override
	protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
		return new LayoutParams1(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
	}

	@Override
	protected ViewGroup.LayoutParams generateLayoutParams(
			ViewGroup.LayoutParams p) {
		return new LayoutParams1(p);
	}

	@Override
	public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
		return new LayoutParams1(this.getContext(), attrs);
	}

	BlockingQueue<CtrlEvent> queueCtrlEvent = new LinkedBlockingQueue<>();
	public static class CtrlEvent {
		public static final int type_asix = 0;
		public static final int type_btn = 1;
		public static final int status_down = 0;
		public static final int status_up = 1;
		public int type;
		public int idx;
		public int status;

		public boolean equalsSrc(CtrlEvent e) {
			return this.idx == e.idx && this.type == e.type;
		}
	}

	public BlockingQueue<CtrlEvent> getCtrlEventQueue() {
		return queueCtrlEvent;
	}

	public int getAsixProgress(int idx) {
		// return this.asixList.get(idx).getProgress();
		return asixesStatus[idx];
	}

	public boolean getButtonPressed(int idx) {
		// return this.btnList.get(idx).isPressed();
		return btnsStatus[idx] == 1;
	}

	int[] btnsStatus = new int[32];
	int[] asixesStatus = new int[8];
	public long lasttime = System.currentTimeMillis();
	public int lastChangedBtnIdx;
	public int lastChangedAsixIdx;

	void btnPressDown(int btnId) {
		int idx = btnId - INDEX_START_BTN;
		btnsStatus[idx] = 1;
		lastChangedBtnIdx = idx;
		lasttime = System.currentTimeMillis();
//		CtrlEvent t = new CtrlEvent();
//		t.idx = btnId - INDEX_START_BTN;
//		t.type = CtrlEvent.type_btn;
//		t.status = CtrlEvent.status_down;
//		Log.i("vjoy", "压下按钮" + t.idx);
//		queueCtrlEvent.add(t);
	}
	void btnPressUp(int btnId) {
		int idx = btnId - INDEX_START_BTN;
		btnsStatus[idx] = 0;
		lastChangedBtnIdx = idx;
		lasttime = System.currentTimeMillis();
//		CtrlEvent t = new CtrlEvent();
//		t.idx = btnId - INDEX_START_BTN;
//		t.type = CtrlEvent.type_btn;
//		t.status = CtrlEvent.status_up;
//		Log.i("vjoy", "释放按钮" + t.idx);
//		queueCtrlEvent.add(t);
	}

	void asixDrag(int asixId, int progress) {
		int idx = asixId - INDEX_START_ASIX;
		asixesStatus[idx] = progress;
		lastChangedAsixIdx = idx;
		lasttime = System.currentTimeMillis();
//		CtrlEvent t = new CtrlEvent();
//		t.idx = asixId - INDEX_START_ASIX;
//		t.type = CtrlEvent.type_asix;
//		t.status = progress;
//		Log.i("vjoy", "拖动轴1" + idx);
//		queueCtrlEvent.add(t);
	}
	void asixDrag(int asixId) {
		int idx = asixId - INDEX_START_ASIX;
		asixesStatus[idx] = this.asixList.get(idx).getProgress();
		lastChangedAsixIdx = idx;
		lasttime = System.currentTimeMillis();
//		CtrlEvent t = new CtrlEvent();
//		t.idx = asixId - INDEX_START_ASIX;
//		t.type = CtrlEvent.type_asix;
//		t.status = this.asixList.get(t.idx).getProgress();
//		Log.i("vjoy", "拖动轴2" + idx);
//		queueCtrlEvent.add(t);

	}
}
